import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Layout from "../components/layout/index.vue"
import local from '@/utlis/local.js'


//静态路由
const routes = [
  {
    //登录
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    //注册
    path: '/register',
    name: 'Register',
    component: ()=>import("@/views/Register.vue")
  },
  {
    //首页
    path:'/',
    name:'Layout',
    component: Layout,
    leftMenu:{
      index:'/',
      text:"后台首页",
      icon:""//图标
    },
    children:[
      {
        path:'',
        component:()=>import("@/views/Home.vue")
      },
    ],
  },
]

//动态路由
export const asyncRouter=[
  //账户管理
  {
    path:"/AccountAnagement",
    name:'AccountAnagement',
    redirect:'/AccountAnagement/AccountList',
    leftMenu:{
      index:'/AccountAnagement',
      text:"知识库一",
      icon:'',//图标
      hasChildren:true//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:'/AccountAnagement/AccountList',
        name:'AccountList',
        leftMenu:{
          index:'/AccountAnagement/AccountList',
          text:"新增vxe表格",
          icon:''//图标
        },
        component:()=>import('@/views/AccountAnagement/AccountList.vue')
      },
      {
        path:'/AccountAnagement/AccountNumber',
        name:'AccountNumber',
        leftMenu:{
          index:'/AccountAnagement/AccountNumber',
          text:"打字机效果",
          icon:''//图标
        },
        component:()=>import('@/views/AccountAnagement/AccountNumber.vue')
      },
      {
        path:'/AccountAnagement/AddAccount',
        name:'AddAccount',
        leftMenu:{
          index:'/AccountAnagement/AddAccount',
          text:"vxe表单",
          icon:''//图标
        },
        component:()=>import('@/views/AccountAnagement/AddAccount.vue')
      },
      {
        path:'/AccountAnagement/ChangePassword',
        name:'ChangePassword',
        leftMenu:{
          index:'/AccountAnagement/ChangePassword',
          text:"步骤条加表单",
          icon:''//图标
        },
        component:()=>import('@/views/AccountAnagement/ChangePassword.vue')
      },
      {
        path:'/AccountAnagement/48545',
        name:'48545',
        leftMenu:{
          index:'/AccountAnagement/48545',
          text:"测试纠错",
          icon:''//图标
        },
        component:()=>import('@/views/AccountAnagement/48545.vue')
      },
    ],
  },
//个人消费管理
  {
    path:"/Consunmption",
    name:'Consunmption',
    leftMenu:{
      index:'/Consunmption',
      text:"知识库二",
      icon:'',//图标
      hasChildren:true//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:'/Consunmption/ConsunmptionList',
        name:'ConsunmptionList',
        leftMenu:{
          index:'/Consunmption/ConsunmptionList',
          text:"筛选加添加表格",
          icon:''//图标
        },
        component:()=>import('@/views/Consunmption/ConsunmptionList.vue')
      },
      {
        path:'/Consunmption/ConsunmptionDetails',
        name:'ConsunmptionDetails',
        leftMenu:{
          index:'/Consunmption/ConsunmptionDetails',
          text:"富文本",
          icon:''//图标
        },
        component:()=>import('@/views/Consunmption/ConsunmptionDetails.vue')
      },
    ],
  },
  //个人生活管理
  {
    path:"/LifeManagement",
    name:'LifeManagement',
    leftMenu:{
      index:'/LifeManagement',
      text:"知识库三",
      icon:'',//图标
      hasChildren:true//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:'/LifeManagement/DayLife',
        name:'DayLife',
        leftMenu:{
          index:'/LifeManagement/DayLife',
          text:"vxe表格带表单按钮组",
          icon:''//图标
        },
        component:()=>import('@/views/LifeManagement/DayLife.vue')
      },
      {
        path:'/LifeManagement/WeekLife',
        name:'WeekLife',
        leftMenu:{
          index:'/LifeManagement/WeekLife',
          text:"element表单",
          icon:''//图标
        },
        component:()=>import('@/views/LifeManagement/WeekLife.vue')
      },
      {
        path:'/LifeManagement/MonthLife',
        name:'MonthLife',
        leftMenu:{
          index:'/LifeManagement/MonthLife',
          text:"小知识库",
          icon:''//图标
        },
        component:()=>import('@/views/LifeManagement/MonthLife.vue')
      },
      {
        path:'/LifeManagement/YearLife',
        name:'YearLife',
        leftMenu:{
          index:'/LifeManagement/YearLife',
          text:"element表格头部弹性布局",
          icon:''//图标
        },
        component:()=>import('@/views/LifeManagement/YearLife.vue')
      },
    ],
  },
//知识库管理
  {
    path:"/Repository",
    name:'Repository',
    roles:['admin'],
    leftMenu:{
      index:'/Repository',
      text:"知识库四",
      icon:'',
      hasChildren:true//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:'/Repository/PersonageKonwledge',
        name:'PersonageKonwledge',
        leftMenu:{
          index:'/Repository/PersonageKonwledge',
          text:"导入导出及添加",
          icon:''//图标
        },
        component:()=>import('@/views/Repository/PersonageKonwledge.vue')
      },
      {
        path:'/Repository/DayAddKonwledge',
        name:'DayAddKonwledge',
        leftMenu:{
          index:'/Repository/DayAddKonwledge',
          text:"选项卡时间线轮播手风琴",
          icon:''//图标
        },
        component:()=>import('@/views/Repository/DayAddKonwledge.vue')
      },
      {
        path:'/Repository/WeekAddKonwledge',
        name:'WeekAddKonwledge',
        leftMenu:{
          index:'/Repository/WeekAddKonwledge',
          text:"vxe表格头部弹性布局",
          icon:''//图标
        },
        component:()=>import('@/views/Repository/WeekAddKonwledge.vue')
      },
      {
        path:'/Repository/MonthAddKonwledge',
        name:'MonthAddKonwledge',
        leftMenu:{
          index:'/Repository/MonthAddKonwledge',
          text:"高德API",
          icon:''//图标
        },
        component:()=>import('@/views/Repository/MonthAddKonwledge.vue')
      },
      {
        path:'/Repository/YearAddKonwledge',
        name:'YearAddKonwledge',
        leftMenu:{
          index:'/Repository/YearAddKonwledge',
          text:"openlayers地图",
          icon:''//图标
        },
        component:()=>import('@/views/Repository/YearAddKonwledge.vue')
      },
    ],
  },
  //工作管理
  {
    path:"/WorkSheet",
    name:'WorkSheet',
    roles:['admin'],
    leftMenu:{
      index:'/WorkSheet',
      text:"知识库五",
      icon:'',
      hasChildren:true//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:'/WorkSheet/WorkList',
        name:'WorkList',
        leftMenu:{
          index:'/WorkSheet/WorkList',
          text:"高德3D",
          icon:''//图标
        },
        component:()=>import('@/views/WorkSheet/WorkList.vue')
      },
      {
        path:'/WorkSheet/DayWorkList',
        name:'DayWorkList',
        leftMenu:{
          index:'/WorkSheet/DayWorkList',
          text:"echarts",
          icon:''//图标
        },
        component:()=>import('@/views/WorkSheet/DayWorkList.vue')
      },
      {
        path:'/WorkSheet/WeekWorkList',
        name:'WeekWorkList',
        leftMenu:{
          index:'/WorkSheet/WeekWorkList',
          text:"前端知识点",
          icon:''//图标
        },
        component:()=>import('@/views/WorkSheet/WeekWorkList.vue')
      },
      {
        path:'/WorkSheet/MonthWorkList',
        name:'MonthWorkList',
        leftMenu:{
          index:'/WorkSheet/MonthWorkList',
          text:"当月工作总结",
          icon:''//图标
        },
        component:()=>import('@/views/WorkSheet/MonthWorkList.vue')
      },
      {
        path:'/WorkSheet/YearWorkList',
        name:'YearWorkList',
        leftMenu:{
          index:'/WorkSheet/YearWorkList',
          text:"word识别",
          icon:''//图标
        },
        component:()=>import('@/views/WorkSheet/YearWorkList.vue')
      },
    ],
  },
  {
    path:"/Model3D",
    name:'Model3D',
    leftMenu:{
      index:'/Model3D',
      text:"3D模型展示",
      icon:'',//图标
      hasChildren:false//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:"",
        component:()=>import('@/views/modelview/Model3D.vue')
      }
    ],
  },
  {
    path:"/404",
    name:'404',
    leftMenu:{
      index:'/404',
      text:"找不到页面",
      icon:'',//图标
      hasChildren:false//是否有子页面
    },
    component:Layout,//显示位置
    children:[
      {
        path:"",
        component:()=>import('@/views/404/404.vue')
      }
    ],
  },
]


const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 动态角色路由权限分配，vue2
// export const filterRouter=(routes, role)=>{
//   return routes.filter((item)=>{
//     if(!item.roles){
//       return true
//     }else{
//       if(item.roles.includes(role)){
//         if(item.children){
//           item.children=filterRouter(item.children,role)
//         }else{
//           return true
//         }
//       }else{
//         return false
//       }
//     }
//   })
// }
// 动态添加路由,vue3
export const creteRoutes=()=>{
  console.log("动态添加路由")
  asyncRouter.forEach((item)=>{
    if(!item.roles){
       router.addRoute(item)
    }else{
      if(item.roles==local.get('roles')){
        router.addRoute(item)
      }
    }
  })
  // 从本地存储获取角色 动态加载权限
  // const rst=filterRouter(asyncRouter,local.get('roles'))
  // router.addRoutes(rst);//vue2可以直接添加多个动态路由
  // router.addRoute(asyncRouter)//vue3只能一个一个的添加
}
// 主动调一次 防止刷新 只会在页面刷新的时候执行一次,此后不会再执行。防止空白页
creteRoutes()


router.beforeEach((to,from,next)=>{
    //to到哪里去 from从哪里来 
    console.log("此路是我开")
    const tk=local.get('tk')
    if(tk){
      next()//有用户放行
    }else if(to.path=='/login'||to.path=='/register'){
      next()//去登录页，放行
    }else{
      next('/login')//没有用户，就去登录
    }
})


export default router
